﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.EntityFrameworkCore;
using NPOI.SS.Formula.Functions;

namespace SheepCommon.API.SheepFarmHomepage.RealTimeStorage
{
    public class RealTimeStorageSumQueryHandler : IRequestHandler<RealTimeStorageSumQueryCommand, Result>
    {
        private readonly IBaseRepository<SheepArchives> _sheepArchivesRepository;

        public RealTimeStorageSumQueryHandler(IBaseRepository<SheepArchives> sheepArchivesRepository)
        {
            _sheepArchivesRepository = sheepArchivesRepository;
        }
        /// <summary>
        /// 羊场总存栏
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(RealTimeStorageSumQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new Result();
            int sum = 0;
            try
            {
                var ealTimeStorageSumQuery = await(await _sheepArchivesRepository.GetAll()).Where(c=>c.CottageId!=null&&c.FieldId!=null).ToListAsync();
                sum = ealTimeStorageSumQuery.Count();
                result.Data = sum;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
